package com.yc.snacknet_vue.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.yc.snacknet_vue.bean.GoodsType;
import com.yc.snacknet_vue.biz.IGoodsTypeBiz;
import com.yc.snacknet_vue.biz.Impl.GoodsTypeBizImpl;
import com.yc.snacknet_vue.utils.RequestToObject;

/**
 * 商品类型业务控制层
 * @author UNO
 * @date 2021年8月7日
 * Email 2759451734@qq.com
 */
@WebServlet("/type/*")
public class GoodsTypeController extends BaseServlet{

	private static final long serialVersionUID = 1L;

	//前台查询所有上架的商品类型
	protected void finds(HttpServletRequest request, HttpServletResponse response) throws IOException {
		IGoodsTypeBiz biz = new GoodsTypeBizImpl();
		List<GoodsType> list = biz.finds();
		if( null == list || list.isEmpty()) {  //查询无数据
			this.send(response, 500, null);
			return;
		}
		this.send(response, 200, list);
	}
	
	//后台查询所有的商品类型
	protected void findAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
		IGoodsTypeBiz biz = new GoodsTypeBizImpl();
		List<GoodsType> list = biz.findAll();
		if( null == list || list.isEmpty()) {  //查询无数据
			this.send(response, 500, null);
			return;
		}
		this.send(response, 200, list);
	}
	
	//添加商品类型
	protected void add(HttpServletRequest request, HttpServletResponse response) throws IOException {
		GoodsType type = RequestToObject.getParamesToObject(GoodsType.class, request);
		if( null == type) {
			this.send(response, 501, null);//请求 无商品礼物i下信息
			return;
		}
		IGoodsTypeBiz biz = new GoodsTypeBizImpl();
		int result = biz.add(type);
		if( result > 0) {  //添加成功
			this.send(response, 200);
			return;
		}
		this.send(response, 500); //添加失败
	}
	
	//更新商品类型
	protected void update(HttpServletRequest request, HttpServletResponse response) throws IOException {
		GoodsType type = RequestToObject.getParamesToObject(GoodsType.class, request);
		if( null == type) {
			this.send(response, 501, null);//请求 无商品信息
			return;
		}
		IGoodsTypeBiz biz = new GoodsTypeBizImpl();
		int result = biz.update(type);
		if( result > 0) {  //更新成功
			this.send(response, 200);
			return;
		}
		this.send(response, 500); //更新失败
	}

		
}
